import CellFunctions from "./CellFunctions";
import GameManager from "../Manager/GameManager";
import SpriteManager from "../Manager/SpriteManager";
import TipPanel from "../UI/TipPanel";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Cell extends cc.Component {
    @property(cc.Sprite)
    Image_item:cc.Sprite = null;

    @property
    x:number = 0;
    @property
    y:number = 0;

    @property
    id:number = 1;

    @property
    target_y:number = 0;

    @property
    number:number = 0;

    @property
    cellSize:number = 0;

    // @property
    // isTop:boolean = false;

    @property
    isNew:boolean = false;

    

    onLoad () {
        this.node.on(cc.Node.EventType.MOUSE_DOWN, this.onMouseDown, this);
        // this.node.on(cc.Node.EventType.MOUSE_UP, this.onMouseUp, this);
        // this.node.on(cc.Node.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
    }

    onMouseDown(event)
    {
        let mouseType = event.getButton();
        if (mouseType === cc.Event.EventMouse.BUTTON_LEFT) {
            // 鼠标左键按下
            // let mousePoint = event.getLocation();
            // let localPoint = this.node.convertToNodeSpace(mousePoint);

            // TipPanel.Instance.Show("鼠标左键按下:" + String(GameManager.Instance.keyCode));

            cc.log(this);
            return;

            if(GameManager.Instance.keyCode == cc.macro.KEY.num1)
            {
                this.id = 1;
            }
            else if(GameManager.Instance.keyCode == cc.macro.KEY.num2)
            {
                this.id = 2;
            }
            else if(GameManager.Instance.keyCode == cc.macro.KEY.num3)
            {
                this.id = 3;
            }
            else if(GameManager.Instance.keyCode == cc.macro.KEY.num4)
            {
                this.id = 4;
            }
            else if(GameManager.Instance.keyCode == cc.macro.KEY.num5)
            {
                this.id = 5;
            }
            else
            {
                this.id = 0;
            }

            this.RefreshUI();
        }
            
        // } else if (mouseType === cc.Event.EventMouse.BUTTON_MIDDLE) {
        //     // 鼠标中键按下
        //     TipPanel.Instance.Show("鼠标中键按下");
        // } else if (mouseType === cc.Event.EventMouse.BUTTON_RIGHT) {
        //     // 鼠标右键按下
        //     TipPanel.Instance.Show("鼠标右键按下");
        // }
    }

    start () {

    }

    public Init(_id:number, _x:number, _y:number)
    {
        this.id = _id;
        this.x = _x;
        this.y = _y;
        this.cellSize = GameManager.Instance.cellSize;

        this.node.name = (this.x + 1) + "_" + (this.y + 1);
        this.node.width = this.cellSize;
        this.node.height = this.cellSize;

        this.node.parent = GameManager.Instance.Node_2D;

        this.Refresh();
    }

    public Refresh()
    {
        this.RefreshPos();
        this.RefreshUI();
    }

    public RefreshUI()
    {
        this.Image_item.node.active = this.id > 0;
        let spriteFrame = SpriteManager.Instance.spriteFrameLists.spriteFrameList[this.id - 1];
        this.Image_item.spriteFrame = spriteFrame;

        // 元素图片适配
        let padding = 16;
        let os = spriteFrame.getOriginalSize();
        if (os.width > os.height) {
            this.Image_item.node.width = this.node.width - padding;
            this.Image_item.node.height = this.Image_item.node.width * (os.height / os.width);
        }
        else
        {
            this.Image_item.node.height = this.node.width - padding;
            this.Image_item.node.width = this.Image_item.node.height * (os.width / os.height);
        }
    }

    public RefreshPos()
    {
        var pos_x = this.cellSize * this.x - GameManager.Instance.width / 2  + this.cellSize / 2;
        var pos_y = this.cellSize * this.y - GameManager.Instance.height_screen / 2 + this.cellSize / 2 + GameManager.Instance.padding_bottom;
        this.node.setPosition(pos_x, pos_y);
    }

    public Set(_x:number, _y:number)
    {
        GameManager.Instance.cellList[this.x][this.y] = null;

        this.x = _x;
        this.y = _y;

        GameManager.Instance.cellList[this.x][this.y] = this;
    }

    public Dead()
    {
        GameManager.Instance.cellList[this.x][this.y] = null;
        
        cc.tween(this.node)
        .to(0.3, { opacity: 80})
        .call(() => {
            this.node.destroy();
        })
        .start()
    }
}
